module.exports = {

	get: {

		'/order/:orderId([a-zA-Z0-9]{32})/check/pay': async ctx => {
			const {type, userId} = await ctx.allow('wxapp', 'store', 'inside')
			const {orderId} = ctx.params
			switch(type) {
				case 'wxapp':
					return await ctx.handles.order.wxAppCheckOrderPay(orderId, userId)
				break
				case 'store':
					return await ctx.handles.order.storeCheckOrderPay(orderId, storeId)
				break
				case 'inside':
					return await ctx.handles.order.insideCheckOrderPay(orderId)
				default:
					return this.reject('-2002', 'user type is invalid')
			}
		}

	},

	post: {

		'/order/wxapp': async ctx => {
			await ctx.allow('wxapp')
			const {type, userId, data} = ctx.user
			let orderId = ''
			//判断是否是已存在且未支付的订单
			if(!ctx.postData.orderId) {
				const {storeId, tableNum, orderPayMode, orderCouponData,  orderRemark, orderGoodsData, formId} = ctx.postData
				const {userName, userRealName, userPhone} = await ctx.handles.user.getWxUserDataByUserId(userId)
				const {storeName, storePayMode, storeFreeze, storeAutoOrder} = await ctx.handles.store.getStoreDataByStoreId(storeId)
				//检查商户支持的支付方式
				const _storePayMode = storePayMode.split(',')
				if(_storePayMode.length == 0)
					return ctx.error('-2049', 'store not support any pay mode')
				if(_storePayMode.indexOf(orderPayMode.toString()) == -1)
					return ctx.error('-2050', 'not support pay mode')
				if(storeFreeze)
					return ctx.error('-2011', 'store is freeze')
				const {orderTotal, orderPayTotal, orderGoods, orderCoupon} = await ctx.handles.order.preprocessOrder({storeId, tableNum, orderPayMode, orderRemark, userId, orderCouponData, orderGoodsData})
				orderId = await ctx.handles.order.createOrder({userType: 0, userId, userName, userRealName, userPhone, storeId, tableNum, orderPayMode, orderTotal, orderPayTotal, orderGoods, orderCoupon, orderRemark})
				switch(parseInt(orderPayMode)) {

					//现金支付
					case 0:
						await ctx.handles.order.cashPayOrder(orderId)
						if(storeAutoOrder == 1) {
							const printers = await ctx.handles.store.getPrinters(storeId)
							if(printers.length == 0)
								console.error(`${storeId} not use printer`)
							const orderData = await ctx.handles.order.takeOrder(orderId, printers)
							for(let printer of printers) {
								const printContent = await ctx.handles.order.buildPrintContent({storeName}, orderData, printer.templateContent)
								await ctx.handles.printer.addTask('order', printer.printerCode, orderId, printContent)
							}
							ctx.handles.sys.sendTemplateMsg({
								app: 'gtClient',
								template: 'orderTaked',
								wxUserId: data.wxUserId,
								page: '/pages/index/index',
								formId,
								data: [orderId, `${storeName} ${tableNum}`, '已成功接单', `${orderPayTotal / 100}元`, ctx.common.timestamp2str(Date.now(), true, true, true, true, true, true)]
							}).catch((err) => {
								ctx.common.alertMsg('模板消息发送失败', `发送给 ${userId} 的 ${orderId} 订单确认消息未发送成功 ${formId}`, err)
								ctx.common.writeLog('error', `send template msg failed ${formId}`, false, err)
							})
						}
						return {orderId}
					break

					//微信支付
					case 1:
						const prePayData = await ctx.handles.order.wxPayOrder(orderId, {wxUserId: data.wxUserId, userIP: ctx.originIP, storeData: {storeId, storeName}, orderPayTotal, orderCoupon})
						return {orderId, prePayData}
					break

					default:
						return ctx.error('-2050', 'not support pay mode')

				}
			}
			else {
				orderId = ctx.postData.orderId
				switch(parseInt(ctx.postData.orderPayMode)) {

					//微信支付
					case 1:
						const prePayData = await ctx.handles.order.reWxPayOrder(orderId, userId)
						return {orderId, prePayData}
					break

					default:
						return ctx.error('-2060', 'not support this repay mode')
				}
			}
				
		},

		'/order/print': async ctx => {

		}

	},

	delete: {

		'/order/cancel/:orderId': async ctx => {
			const {userId} = await ctx.allow('wxapp', 'store', 'inside')
			const {orderId} = ctx.params
			await ctx.handles.order.cancelWxPayOrder(orderId, userId)
			return 1
		}

	}

}